import { setToken, http, clearToken } from '@/utiles'
import * as types from '../actionsType'


const login = (loginData) => {
  return async (dispatch) => {
    const res = await http.post('/authorizations', loginData)
    const {
      data: { token },
      message,
    } = res.data
    
    // 数据请求成功就存入本地，并更新redux的store数据
    if (message === 'OK') {
      setToken(token)
      dispatch({
        type: types.GET_TOKEN,
        payload: token,
      })
    }
  }
}
const logout = () => {
  return (dispatch) => {
    // 清除token
    dispatch({
      type: types.CLEAR_TOKEN,
    })
    clearToken()
    // 清除个人信息
    dispatch({
      type: types.CLEAR_INFO
    })
  }
}

export { login, logout }
